import path, { resolve } from "path";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import esbuild from "rollup-plugin-esbuild";
import vuePlugin from "rollup-plugin-vue";
import postcss from "rollup-plugin-postcss";
import vueSetupExtend from "unplugin-vue-setup-extend-plus/rollup";

const __dirname = path.resolve();

export default {
  input: "packages/pkg1/index.ts",
  output: {
    format: "es",
    // 指定所有生成的 chunk 被放置在哪个目录中
    dir: "dist/lc-ui",
    exports: "named",
    // 当 output.preserveModules 值为 true 时，输入模块的目录路径应从 output.dir 路径中剥离出来。
    preserveModules: true,
    // 将保留的模块放置在根路径下的此路径下
    preserveModulesRoot: resolve(__dirname, "packages", "lc-ui"),
    name: "lib",
    sourcemap: true,
    entryFileNames: `[name].js`,
  },
  external: ["vue"],
  plugins: [
    vueSetupExtend(),
    nodeResolve({
      extensions: [".mjs", ".js", ".json", ".ts"],
    }),
    esbuild({
      // All options are optional
      include: /.[jt]sx?$/, // default, inferred from `loaders` option
      // exclude: /node_modules/, // default
      sourceMap: process.env.NODE_ENV === "production",
      minify: process.env.NODE_ENV === "production",
      target: "es2018", // default, or 'es20XX', 'esnext'
      // jsx: 'transform', // default, or 'preserve'
      // jsxFactory: 'React.createElement',
      // jsxFragment: 'React.Fragment',
      // Like @rollup/plugin-replace
      // define: {
      //   'process.env.NODE_ENV': JSON.stringify('production'),
      // },
      loaders: {
        ".vue": "ts",
      },
      // tsconfig: 'tsconfig.json', // default
    }),
    //处理vue文件
    vuePlugin({
      // 引用的vue插件，即上述引入的插件使用一遍，以及添加一些选项
      include: /.vue$/,
      // 把单文件组件中的样式，插入到html中的style标签
      css: false,
      preprocessStyles: true,
      // 把组件转换成 render 函数
      //compileTemplate: true
    }),
    //rollup-plugin-postcss支持Sass、Less和Stylus
    postcss({
      // 把 css 放到和js同一目录
      extract: true,
      // modules: true,
    }),
  ],
};
